package com.qifan.alb2b.action.front;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qifan.alb2b.exception.BusinessException;
import com.qifan.alb2b.service.RouteInfoService;
import com.qifan.alb2b.service.resource.IResource;
import com.qifan.alb2b.service.resource.ResTable;
import com.qifan.alb2b.service.resource.ResourceFactory;
import com.qifan.alb2b.service.vo.ResourceVo;
import com.qifan.alb2b.service.vo.RouteDetailVo;
import com.qifan.alb2b.service.vo.RouteVo;
import com.qifan.alb2b.util.Constants;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.Objecter;
import com.qifan.alb2b.util.Page;

/**
 * 定制添加资源相关
 * @author hc24
 *
 */
@Controller
@RequestMapping("/res")
public class ResourceController {
	private ResourceFactory resourceFactory;
	private RouteInfoService routeInfoService;

	public void setResourceFactory(ResourceFactory resourceFactory) {
		this.resourceFactory = resourceFactory;
	}
	
	public void setRouteInfoService(RouteInfoService routeInfoService) {
		this.routeInfoService = routeInfoService;
	}


	/**
	 * 去展示要设置的资源
	 * @param request
	 * @param cityId
	 * @param resType
	 * @return
	 */
	@RequestMapping("/toResList.usr")
	public String toResList(HttpServletRequest request,String cityId,int resType,String routeDetailId){	
		HttpSession session=request.getSession(true);
		RouteVo routeVo=(RouteVo) session.getAttribute(Constants.SESSION_ROUTE_VO);
		if(routeVo==null)
			throw new BusinessException("请重新选择线路");
		
		//当前已经选择的资源列表
		List<String> resIdList=new ArrayList<String>();
		List<RouteDetailVo> detailList= routeVo.getDetailVoList();
		int i=0;
		for(;i<detailList.size();i++){
			RouteDetailVo rdv=detailList.get(i);
			if(routeDetailId.equals(rdv.getRouteDetail().getDetailId())){//找到当前线路详情
				List<ResourceVo> resList=rdv.getResList();
				for(int j=0;j<resList.size();j++){
					if(resList.get(j).getResType()==resType){
						resIdList.add(resList.get(j).getResId());
					}					
				}
				break;//找到了当前线路详情则结束循环
			}//end if
		}//end for
		if(resType==Constants.RES_TYPE_HOTEL&&(i+1)>routeVo.getRoute().getNight()){
			request.setAttribute("exception", "最后一天不能添加住宿！");			
			return "pages/common/error2";
		}
		
		request.setAttribute("resIdList", resIdList);
		
		IResource resource= resourceFactory.newResouce(resType);
		request.setAttribute("cityId", cityId);
		request.setAttribute("resType", resType);
		request.setAttribute("routeDetailId", routeDetailId);
		request.setAttribute("ths", resource.getHeader());
		request.setAttribute("singleSelect", resource.isSingleSelect());
		return "pages/front/book/set_res";
	}
	
	/**
	 * 根据条件查询资源数据
	 * @param resType 资源类型
	 * @param cityId  城市ID
	 * @param dgm 分页数据
	 * @return 
	 */
	@RequestMapping("/queryResList.usr")
	@ResponseBody
	public Page queryResList(HttpServletRequest request,int resType,String cityId,DataGridModel dgm){
		HttpSession session=request.getSession(true);
		RouteVo routeVo=(RouteVo) session.getAttribute(Constants.SESSION_ROUTE_VO);
		if(routeVo==null)
			throw new BusinessException("请重新选择线路");
		
		IResource resource= resourceFactory.newResouce(resType);
		ResTable rt = resource.getResTable(cityId, routeVo.getGroupType(), dgm);
		return rt.getPage();
	}
	
	/**
	 * 根据条件查询资源详细价格信息
	 * @param request
	 * @param resType
	 * @param resId
	 * @param cityId
	 * @return
	 */
	@RequestMapping("/showDetail.usr")
	public String showDetail(HttpServletRequest request,int resType,String resId,String cityId){
		HttpSession session=request.getSession(true);
		RouteVo routeVo=(RouteVo) session.getAttribute(Constants.SESSION_ROUTE_VO);
		if(routeVo==null)
			throw new BusinessException("请重新选择线路");
		IResource resource= resourceFactory.newResouce(resType);
		ResTable rt=resource.getResDetailTable(cityId, resId, routeVo.getGroupType());
		request.setAttribute("ths", rt.getHeader());
		request.setAttribute("datas", rt.getPage().getRows());
		request.setAttribute("remarkList", rt.getRemarkList());
		return "pages/front/book/res_detail";
	}
	
	/**
	 * 更新RouteVo里面的标配价格和当前线路详情里面的资源列表<br/>
	 * 如果resIds不为空，则在执行新增或者是更新操作;
	 * 如果resIds为空，则在后台要先判断是否允许删除，对于标配资源是不允许删除的，
	 * 对于非标配资源则执行删除或这也可能什么也不做操作。
	 * @param resIds
	 * @param routeDetailId
	 * @param resType
	 * @return
	 */
	@RequestMapping("updateRes.usr")
	@ResponseBody
	public Map<String, Object> updateRes(HttpServletRequest request, String[] resIds,String routeDetailId,int resType){
		HttpSession session=request.getSession(true);
		RouteVo routeVo=(RouteVo) session.getAttribute(Constants.SESSION_ROUTE_VO);
		if(routeVo==null)
			throw new BusinessException("请重新选择线路");
		IResource resource= resourceFactory.newResouce(resType);
		resource.addOrUpdate(routeVo, routeDetailId, resIds);//调用具体的资源更新策略
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("success", true);
		return map;
	}
	
	/**
	 * 增加晚宿功能
	 * @param request
	 * @return
	 */
	@RequestMapping("addNight.usr")
	@ResponseBody
	public Map<String, Object> addNight(HttpServletRequest request,String routeDetailId){
		HttpSession session=request.getSession(true);
		RouteVo routeVo=(RouteVo) session.getAttribute(Constants.SESSION_ROUTE_VO);
		if(routeVo==null)
			throw new BusinessException("请重新选择线路");
		routeInfoService.addNight(routeVo, routeDetailId);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("success", true);
		return map;
	}
	
	/**
	 * 删除晚宿功能
	 * @param request
	 * @return
	 */
	@RequestMapping("delNight.usr")
	@ResponseBody
	public Map<String, Object> delNight(HttpServletRequest request,String routeDetailId){
		HttpSession session=request.getSession(true);
		RouteVo routeVo=(RouteVo) session.getAttribute(Constants.SESSION_ROUTE_VO);
		if(routeVo==null)
			throw new BusinessException("请重新选择线路");
		routeInfoService.delNight(routeVo, routeDetailId);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("success", true);
		return map;
	}
	
	
}
